package reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reggie.dto.DishDto;
import reggie.entity.Dish;
import reggie.entity.DishFlavor;
import reggie.mapper.DishMapper;
import reggie.service.DishFlavorService;
import reggie.service.DishService;
import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {


    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品的同时，把口味数据保存进flavor表
     * @param dishDto
     */
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品信息到dish表
        this.save(dishDto);

        Long dishDtoId = dishDto.getId();//菜品id

        //将菜品口味数据都加上菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();

        for (int i = 0; i < flavors.size(); i++) {

            DishFlavor flavor = flavors.get(i);

            flavor.setDishId(dishDtoId);

            flavors.set(i,flavor);
        }

        //保存菜品口味数据到菜品口味表
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void deleteWithFlavor(List<Long> ids) {
        if (!ids.isEmpty()) {
            for (int i = 0; i < ids.size(); i++) {
                Long id = ids.get(i);

                //先根据dishId查询出相应的口味信息
                LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(DishFlavor::getDishId,id);

                //删除口味信息
                dishFlavorService.remove(queryWrapper);

                //删除dish表信息
                removeById(id);
            }
        }
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     * @param id
     * @return
     */
    public DishDto getByIdWithFlover(Long id) {

        //从dish表查询菜品信息
        Dish dish = getById(id);

//        DishDto dishDto = (DishDto) dish;
        DishDto dishDto = new DishDto();

        //将菜品信息拷贝进dishDto对象中
        BeanUtils.copyProperties(dish,dishDto);

        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dish.getId());

        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);

        dishDto.setFlavors(flavors);

        return dishDto;
    }

    /**
     * 更新菜品的同时，把口味数据保存进flavor表
     * @param dishDto
     */
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //1.更新dish表数据
        this.updateById(dishDto);

        //2.清理当前菜品对应的口味数据--dish_flavor表的delete操作
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(queryWrapper);

        //3.添加当前提交过来的口味数据--dish_flavor表的insert操作
        //3.1将菜品口味数据都加上菜品id
        List<DishFlavor> flavors = dishDto.getFlavors();

        for (int i = 0; i < flavors.size(); i++) {

            DishFlavor flavor = flavors.get(i);

            flavor.setDishId(dishDto.getId());

            flavors.set(i,flavor);
        }

        //保存菜品口味数据到菜品口味表
        dishFlavorService.saveBatch(flavors);

    }


}
